package com.xq.jltx.actiontoolsbar;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.view.menu.MenuBuilder;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.Window;
import android.widget.Button;
import android.widget.Toast;

import com.xq.jltx.utils.XqToast;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class SimpleToolBarActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private Button btn1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_tool_bar);
        initToolBar();
        initView();
    }

    private void initToolBar(){
        toolbar = (Toolbar)findViewById(R.id.id_tool_bar);
        //设置toolbar的logo
        toolbar.setLogo(R.mipmap.ic_launcher);
        //设置最左边返回按钮或者说主页按钮
        toolbar.setNavigationIcon(R.drawable.actionbar_back1);
        // Title
        toolbar.setTitle("My Title");
        //设置toolbar的标题栏字体
//        toolbar.setTitleTextAppearance(this,R.style.myTitleTextAppearance);
        // Sub Title
        toolbar.setSubtitle("Sub title");
//        setOverflowButtonAlways();
        //把toolbar设置到Activity中
        setSupportActionBar(toolbar);
//        toolbar.inflateMenu(R.menu.simple_toolbar_menu);
        // Menu item click 的監聽事件一樣要設定在 setSupportActionBar 才有作用
//        toolbar.setOnMenuItemClickListener(onMenuItemClick);
    }

    private void initView(){
        btn1 = (Button)this.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                toolbar.showOverflowMenu();
            }
        });
    }

    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {//MENU键
            if(toolbar != null){
                toolbar.showOverflowMenu();
                return true;
            }

        }
        return super.onKeyUp(keyCode, event);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.simple_toolbar_menu,menu);
        return true;
    }
    @Override
    protected boolean onPrepareOptionsPanel(View view, Menu menu) {
        if (menu != null) {
            if (menu.getClass() == MenuBuilder.class) {
                try {
                    Method m = menu.getClass().getDeclaredMethod("setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e) {
                }
            }
        }
        return super.onPrepareOptionsPanel(view, menu);
    };

    /**
     * 设置menu显示icon
     */
    @Override
    public boolean onMenuOpened(int featureId, Menu menu)
    {

        if (featureId == Window.FEATURE_ACTION_BAR && menu != null)
        {
            if (menu.getClass().getSimpleName().equals("MenuBuilder"))
            {
                try
                {
                    Method m = menu.getClass().getDeclaredMethod(
                            "setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                } catch (Exception e)
                {
                    e.printStackTrace();
                }
            }
        }

        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home){
//            finish();
            XqToast.showToastShortMsg(this,"返回按钮");
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * 通过反射的方式显示actionbar的右上角的按键
     * 通常来说在Actionbar中在条目过多时会显示三个竖着的小点的菜单，但在实机测试的时候发现并不显示，查找资料并测试之后发现问题所在：如果该机器拥有实体的menu键则不在右侧显示溢出菜单，而改为按menu来生成。这样就不利于统一的界面风格。
     * 我们可以改变系统探测实体menu键的存在与否来改变这个的显示
     */
    private void setOverflowButtonAlways()
    {
        try
        {
            ViewConfiguration config = ViewConfiguration.get(this);
            Field menuKey = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey");
            menuKey.setAccessible(true);
            menuKey.setBoolean(config, false);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    private Toolbar.OnMenuItemClickListener onMenuItemClick = new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            String msg = "";
            switch (menuItem.getItemId()) {
                case R.id.home:
                    msg += "Click edit";
                    break;
//                case R.id.action_share:
//                    msg += "Click share";
//                    break;
//                case R.id.action_settings:
//                    msg += "Click setting";
//                    break;
            }

            if(!msg.equals("")) {
                Toast.makeText(SimpleToolBarActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
    };
}
